package OliveEdit;

require Exporter;
use warnings;
use strict;
use OliveStory;

our @ISA       = qw(Exporter);
our @EXPORT    = qw( &deletefeed );

#--------------------------------------------------------------

sub deletefeed {
    my $cui = shift;
    my $w   = $cui->userdata->{wins};
    my $c   = $cui->userdata->{c};

    if($cui->getobj('opts')) {
        $cui->getobj('opts')->focus;
        $cui->getobj('opts')->draw;
        return;
    }

    # build hash of enabled options from config object
    my @values = ();
    my %labels = ();
    my $i = 0;
    while (my $opt = each %{$c->{feeds}}) {
        $values[$i] = $opt;
        $labels{$values[$i]} = $c->{feeds}{$opt}{disp};
        $i++;
    }
    my $height = @values;

    my $opts = $cui->add('opts', 'Window',
                         -border   => 1,
                         -bfg      => 'blue',
                         -title    => 'Remove Feeds',
                         -height   => $height + 6,
                         -width    => 27,
                         -centered => 1,
                        );
    $opts->{list} = $opts->add('obli', 'Listbox',
                               -values => \@values,
                               -labels => \%labels,
                               -multi  => 1,
                               -height => $height,
                               -width  => 30,
                               -y      => 1,
                               -x      => 2,
                               -vscrollbar => 1,
        );
    $opts->{okb} = $opts->add('obok', 'Buttonbox',
                              -y       => -2,
                              -x       => 16,
                              -buttons => [ { -label   => '< OK >',
                                              -value   => 1,
                                              -onpress => sub { $opts->loose_focus;
                                                                saveopts($cui,$opts->{list});
                                                                $c->write;
                                                                refreshlist($cui);
                                                                $cui->delete('opts');
                                                                $cui->draw },
                                            } 
                              ],
        );
    $opts->draw;
    $opts->focus;
}

sub saveopts {
    my ($cui,$o) = @_;
    my $c = $cui->userdata->{c};
    my $d = $cui->userdata->{dbh};
    my @opts    = $o->get;

    # destroy feeds
    foreach my $opt (@opts) {
        # clear out db entries
        my $statement = "DELETE FROM stories WHERE nick = '$opt'";
        my $sth = $d->prepare($statement);
        $sth->execute;
        # delete from HoH
        delete $c->{feeds}{$opt};
    }
}

1;

